package ar.edu.ufasta.presupuestos.visual;

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import ar.edu.ufasta.presupuestos.logica.aplicativo.ControladorSeccionArticulos;
import ar.edu.ufasta.presupuestos.logica.aplicativo.ModeloTablaGenerica;
import ar.edu.ufasta.presupuestos.logica.entidades.articulo.TipoArticulo;


public class VentanaItemsPresupuesto extends JDialog {

	private static final long serialVersionUID = 1L;
	// Variables internas de esta clase
	final String[] nombresColumnas = { "ID", "Nombre", "Descripcion", "Precio",
	"Stock","Tipo" };
	ModeloTablaGenerica modeloArticulos = new ModeloTablaGenerica(nombresColumnas);
	private ControladorSeccionArticulos controladorArticulos = new ControladorSeccionArticulos();
	private int filaSeleccionada = -1;
	private final Hashtable<String, Object> articuloSeleccionado = new Hashtable<String, Object>(); // @jve:decl-index=0:

	private JPanel panelPrincipal = null;
	private JButton botonAceptar = null;
	private JButton botonCancelar = null;
	private JFormattedTextField txtCantidad = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JScrollPane jScrollPane = null;
	private JTable tablaArticulos = null;

	/**
	 * @param owner
	 */
	public VentanaItemsPresupuesto(Frame owner) {
		super(owner);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(387, 308);
		this.setResizable(false);
		this.setTitle("Agregar Articulo");
		this.setContentPane(getPanelPrincipal());
		this.setLocationRelativeTo(null);
		articuloSeleccionado.clear();
		articuloSeleccionado.put("vCodigo", -1);
	}

	/**
	 * This method initializes panelPrincipal
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelPrincipal() {
		if (panelPrincipal == null) {
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = GridBagConstraints.BOTH;
			gridBagConstraints7.gridy = 2;
			gridBagConstraints7.weightx = 1.0;
			gridBagConstraints7.weighty = 1.0;
			gridBagConstraints7.gridheight = 1;
			gridBagConstraints7.gridwidth = 5;
			gridBagConstraints7.insets = new Insets(10, 10, 10, 10);
			gridBagConstraints7.gridx = 0;
			GridBagConstraints gridBagConstraints51 = new GridBagConstraints();
			gridBagConstraints51.gridx = 0;
			gridBagConstraints51.gridwidth = 2;
			gridBagConstraints51.anchor = GridBagConstraints.WEST;
			gridBagConstraints51.gridy = 1;
			jLabel2 = new JLabel();
			jLabel2.setText("Articulos Disponibles:");
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.gridwidth = 5;
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridy = 0;
			jLabel1 = new JLabel();
			jLabel1
					.setText("<html>Seleccione el articulo que desea insertar y haga click en Aceptar</html>");
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 2;
			gridBagConstraints4.anchor = GridBagConstraints.EAST;
			gridBagConstraints4.ipadx = 50;
			gridBagConstraints4.gridy = 3;
			jLabel = new JLabel();
			jLabel.setText("Cantidad:");
			jLabel.setHorizontalAlignment(SwingConstants.TRAILING);
			jLabel.setHorizontalTextPosition(SwingConstants.TRAILING);
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 3;
			gridBagConstraints3.weightx = 0.0D;
			gridBagConstraints3.ipadx = 20;
			gridBagConstraints3.ipady = 0;
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints3.gridx = 4;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 4;
			gridBagConstraints2.insets = new Insets(10, 20, 10, 20);
			gridBagConstraints2.gridy = 4;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.insets = new Insets(10, 20, 10, 20);
			gridBagConstraints1.gridy = 4;
			panelPrincipal = new JPanel();
			panelPrincipal.setLayout(new GridBagLayout());
			panelPrincipal.add(getBotonAceptar(), gridBagConstraints1);
			panelPrincipal.add(getBotonCancelar(), gridBagConstraints2);
			panelPrincipal.add(getTxtCantidad(), gridBagConstraints3);
			panelPrincipal.add(jLabel, gridBagConstraints4);
			panelPrincipal.add(jLabel1, gridBagConstraints5);
			panelPrincipal.add(jLabel2, gridBagConstraints51);
			panelPrincipal.add(getJScrollPane(), gridBagConstraints7);
		}
		return panelPrincipal;
	}

	/**
	 * This method initializes botonAceptar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBotonAceptar() {
		if (botonAceptar == null) {
			botonAceptar = new JButton();
			botonAceptar.setText("Aceptar");
			botonAceptar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if ((filaSeleccionada == -1)||(((Integer)txtCantidad.getValue())<=0))
					{
						MensajeGenerico mensaje = new MensajeGenerico();
						mensaje.setErrorMensaje("Por favor ingrese una cantidad de articulos mayor a 0");
						mensaje.mostrarMensaje(VentanaItemsPresupuesto.this);
					}else
					{
						articuloSeleccionado.clear();
						articuloSeleccionado.put("vCodigo", (Integer)tablaArticulos.getModel().getValueAt(filaSeleccionada, 0));
						articuloSeleccionado.put("vCantidad", Integer.parseInt(txtCantidad.getText()));
						articuloSeleccionado.put("vNombre", tablaArticulos.getModel().getValueAt(filaSeleccionada, 1));
						articuloSeleccionado.put("vPrecio", (Double)tablaArticulos.getModel().getValueAt(filaSeleccionada, 3));
						articuloSeleccionado.put("vTipo", ((TipoArticulo)tablaArticulos.getModel().getValueAt(filaSeleccionada, 6)).getIDValue());
						VentanaItemsPresupuesto.this.setVisible(false);
					}
					
				}
			});
		}
		return botonAceptar;
	}

	/**
	 * This method initializes botonCancelar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBotonCancelar() {
		if (botonCancelar == null) {
			botonCancelar = new JButton();
			botonCancelar.setText("Cancelar");
			botonCancelar
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							articuloSeleccionado.clear();
							articuloSeleccionado.put("vCodigo", -1);
							VentanaItemsPresupuesto.this.setVisible(false);
						}
					});
		}
		return botonCancelar;
	}

	/**
	 * This method initializes txtCantidad
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtCantidad() {
		if (txtCantidad == null) {
			txtCantidad = new JFormattedTextField();
			txtCantidad.setValue(new Integer(0));
		}
		return txtCantidad;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getTablaArticulos());
			// Inicializo la tabla
			try {
				Vector<Object[]> v = new Vector<Object[]>();
				v = controladorArticulos.getRegistrosDonde(1, "ARTICULOS", null, 0);
				modeloArticulos.setRecordList(v);
				modeloArticulos.setColumnMapList(nombresColumnas);
			} catch (Exception e) {
			}
		}
		return jScrollPane;
	}

	/**
	 * This method initializes tablaArticulos
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTablaArticulos() {
		if (tablaArticulos == null) {
			tablaArticulos = new JTable(modeloArticulos);
			TableColumn remove = null;
			TableColumnModel colModel = tablaArticulos.getColumnModel();
			// Quito columnas que no necesito del modelo
			remove = colModel.getColumn(2);
			colModel.removeColumn(remove);
			remove = colModel.getColumn(3);
			colModel.removeColumn(remove);
			remove = colModel.getColumn(3);
			colModel.removeColumn(remove);
			
			ListSelectionModel cellSelectionModel = tablaArticulos
					.getSelectionModel();
			cellSelectionModel
					.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			cellSelectionModel
					.addListSelectionListener(new ListSelectionListener() {
						public void valueChanged(ListSelectionEvent e) {
							int selectedRow = tablaArticulos.getSelectedRow();
							filaSeleccionada = selectedRow;
							//filaSeleccionada = (Integer) tablaArticulos.getValueAt(selectedRow, 0);

						}

					});

		}
		return tablaArticulos;
	}

	public Hashtable<String, Object> getArticuloSeleccionado() {
		return articuloSeleccionado;
	}

} // @jve:decl-index=0:visual-constraint="198,13"
